<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>教务后台管理-登陆</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">

</head>
<body>
<div class="main-body">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>用户更换头像</legend>
    </fieldset>

    <div class="layui-upload" style="margin-left: 30%">
        <button type="button" class="layui-btn" id="test1">上传头像</button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1">
            <p id="demoText"></p>
        </div>
        <div style="width: 95px;">
            <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                <div class="layui-progress-bar" lay-percent=""></div>
            </div>
        </div>
    </div>

    <a name="list-progress"> </a>

    <div style="margin-top: 10px;">
</div>
<div class="footer">
    ©版权所有 2014-2018 叁贰柒工作室<span class="padding-5">|</span><a target="_blank" href="http://www.miitbeian.gov.cn">粤ICP备16006642号-2</a>
</div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    layui.use(['jquery','layer','upload','element'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            upload = layui.upload,
            element = layui.element,
            form = layui.form;
        //获取数据，并将String转化为JSON格式
        var data = JSON.parse(localStorage.getItem('admin'));
        console.log(data);
        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/admin/changeImg?id='+data.id // 实际使用时改成您自己的上传接口即可。
            // , files: {id: data.id} 参数传递
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    layer.msg('上传成功');
                    // console.log(res.data);//path
                    //把后端传入的path放到index页面的img里去
                    $(window.parent.document).find("#headImg").attr('src',res.data);
                }else{
                    return layer.msg('上传失败');
                    $('#demoText').html(''); //置空上传失败的状态
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        })
        });

</script>

</body>
</html>